package business;

import java.util.ArrayList;
import java.util.List;

import org.bson.types.ObjectId;

import models.Group;
import models.forms.GroupForm;
import dao.GroupDao;
import dao.impl.GroupDaoImpl;

public class GroupBusiness {
	private GroupDao groupDao;

	public GroupBusiness() {
		this.groupDao = new GroupDaoImpl();
	}
	
	public List<Group> selectList(Group group){
		return groupDao.selectList(group);
	}
	
	public void save(GroupForm form){
		Group group = new Group();
		group.setName(form.getGroupName());
		group.setPrivate(form.isPrivate());
		List<String> userIds = form.getGroupUsers();
		int size = userIds.size();
		List<ObjectId> oids = new ArrayList<ObjectId>();
		for (int i = 0; i < size; i++) {
			oids.add(new ObjectId((userIds.get(i))));
		}
		group.setUsers(oids);
		groupDao.insert(group);
	}
	
	public List<Group> selectByUserId(String userId){
		return groupDao.selectByUserId(userId);
	}
	
	public List<Group> selectAll(){
		return groupDao.selectAll();
	}
	
	public Group selectById(String groupId){
		return groupDao.selectById(groupId);
	}
}
